<?php
	include_once("../session_ini.php");
	include_once("../model/TareaService.php");
	include_once("../model/utils/Fechas.php");
	
	$service = new TareaService();
	
	$tarea = new Tarea();
	$tarea->id = $_POST["id"];
	$tarea->titulo = utf8_decode($_POST["titulo"]);
	$tarea->descripcion = utf8_decode($_POST["descripcion"]);
	$tarea->deadline = $_POST["deadline"];
	$tarea->id_usuario = $_POST["id_usuario"];
	if($tarea->id_usuario == null || $tarea->id_usuario == "null"){
		$tarea->id_estado = 1;
	}else{
		$tarea->id_estado = 2;
	}
	$tarea->id_historia = $_POST["id_historia"];
	
	if($tarea->id != null){
		$tarea = $service->update($tarea);
		if($tarea != null){
			returnTarea($tarea);
		}else{
			echo "false";
		}
	}else{
		$tarea = $service->save($tarea);
		if($tarea != null){
			returnTarea($tarea);
		}else{
			echo "false";
		}
	}
	
	function returnTarea($tarea){
		$fechas = new Fechas();
	?>
		<tr height="30" valign="middle" id="tarea<?php echo $tarea->id; ?>">
			<td class="titulo"><?php echo utf8_encode($tarea->titulo); ?></td>
			<td class="estado"><?php echo utf8_encode($tarea->estado); ?></td>
			<td class="usuario"><?php echo utf8_encode($tarea->usuario); ?></td>
			<td class="deadline"><?php echo $fechas->convertirFecha($tarea->deadline); ?></td>
			<td align="center" class="editar">
				<a href="<?php echo URL_BASE(); ?>/view/components/editTarea.php?id_usuario=<?php echo $_SESSION["id_usuario"]; ?>&id_historia=<?php echo $hist->id; ?>&id_tarea=<?php echo $tarea->id; ?>" class="crear" title="Editar"><img alt="Editar" src="<?php echo URL_BASE(); ?>/view/images/editar.png" /></a>
				<a href="<?php echo $tarea->id; ?>" title="Finalizar" class="finalizar"><img alt="Finalizar" src="<?php echo URL_BASE(); ?>/view/images/finalizar.png" /></a>
				<a href="<?php echo $tarea->id; ?>" title="Eliminar" class="eliminar"><img alt="Eliminar" src="<?php echo URL_BASE(); ?>/view/images/tachito.png" /></a>
			</td>
			<td align="center"></td>
		</tr>
	<?php
	}
?>